<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>date_format</title>
<meta name="generator" content="DocBook XSL Stylesheets V1.75.2">
<link rel="home" href="index.html" title="Smarty 3 Manual">
<link rel="up" href="language.modifiers.html" title="Chapter 5. Variable Modifiers">
<link rel="prev" href="language.modifier.count.words.html" title="count_words">
<link rel="next" href="language.modifier.default.html" title="default">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="navheader">
<table width="100%" summary="Navigation header">
<tr><th colspan="3" align="center">date_format</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="language.modifier.count.words.html">Prev</a> </td>
<th width="60%" align="center">Chapter 5. Variable Modifiers</th>
<td width="20%" align="right"> <a accesskey="n" href="language.modifier.default.html">Next</a>
</td>
</tr>
</table>
<hr>
</div>
<div class="sect1" title="date_format">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="language.modifier.date.format"></a>date_format</h2></div></div></div>
<p>
     This formats a date and time into the given
     <a class="ulink" href="http://php.net/strftime" target="_top"><code class="varname">strftime()</code></a> format.
     Dates can be passed to Smarty as unix
     <a class="ulink" href="http://php.net/function.time" target="_top">timestamps</a>, 
     <a class="ulink" href="http://php.net/class.DateTime" target="_top">DateTime objects</a>, mysql timestamps
     or any string made up of month day year, parsable by php's
     <a class="ulink" href="http://php.net/strtotime" target="_top"><code class="varname">strtotime()</code></a>.
     Designers can then use <code class="varname">date_format</code> to have complete control of the
     formatting of the date. If the date passed to
     <code class="varname">date_format</code> is empty  and a second parameter is passed,
     that will be used as the date to format.
   </p>
<div class="informaltable"><table border="1">
<colgroup>
<col align="center">
<col align="center">
<col align="center">
<col align="center">
<col>
</colgroup>
<thead><tr>
<th align="center">Parameter Position</th>
<th align="center">Type</th>
<th align="center">Required</th>
<th align="center">Default</th>
<th>Description</th>
</tr></thead>
<tbody>
<tr>
<td align="center">1</td>
<td align="center">string</td>
<td align="center">No</td>
<td align="center">%b %e, %Y</td>
<td>This is the format for the outputted date.</td>
</tr>
<tr>
<td align="center">2</td>
<td align="center">string</td>
<td align="center">No</td>
<td align="center">n/a</td>
<td>This is the default date if the input is empty.</td>
</tr>
</tbody>
</table></div>
<p>
   </p>
<div class="note" title="Note" style="margin-left: 0.5in; margin-right: 0.5in;">
<h3 class="title">Note</h3>
<p>
     Since Smarty-2.6.10 numeric values passed to <code class="varname">date_format</code> are
     <span class="emphasis"><em>always</em></span> (except for mysql timestamps, see
     below) interpreted as a unix timestamp.
    </p>
<p>
     Before Smarty-2.6.10 numeric strings that where also parsable by
     <code class="varname">strtotime()</code> in php (like <code class="literal">YYYYMMDD</code>)
     where sometimes (depending on the underlying implementation of
     <code class="varname">strtotime()</code>) interpreted as
     date strings and NOT as timestamps.
    </p>
<p>
     The only exception are mysql timestamps: They are also numeric
     only and 14 characters long (<code class="literal">YYYYMMDDHHMMSS</code>),
     mysql timestamps have precedence over unix timestamps.
    </p>
</div>
<p>
      </p>
<div class="note" title="Programmers note" style="margin-left: 0.5in; margin-right: 0.5in;">
<h3 class="title">Programmers note</h3>
<p>
     <code class="varname">date_format</code> is essentially a wrapper to PHP's
     <a class="ulink" href="http://php.net/strftime" target="_top"><code class="varname">strftime()</code></a> function.
     You may have more or less conversion specifiers available depending
     on your system's <a class="ulink" href="http://php.net/strftime" target="_top"><code class="varname">strftime()</code></a>
     function where PHP was compiled. Check your
     system's manpage for a full list of valid specifiers. However, a few of
     the specifiers are emulated on Windows. These are: %D, %e, %h, %l, %n, 
     %r, %R, %t, %T.
    </p>
</div>
<p>
  </p>
<div class="example">
<a name="id351528"></a><p class="title"><b>Example 5.8. date_format</b></p>
<div class="example-contents">
<pre class="programlisting">

&lt;?php

$config['date'] = '%I:%M %p';
$config['time'] = '%H:%M:%S';
$smarty-&gt;assign('config', $config);
$smarty-&gt;assign('yesterday', strtotime('-1 day'));

?&gt;

   </pre>
<p>
    This template uses <a class="link" href="language.variables.smarty.html#language.variables.smarty.now" title="{$smarty.now}">
    <em class="parameter"><code>$smarty.now</code></em></a> to get the current time:
   </p>
<pre class="programlisting">

{$smarty.now|date_format}
{$smarty.now|date_format:"%D"}
{$smarty.now|date_format:$config.date}
{$yesterday|date_format}
{$yesterday|date_format:"%A, %B %e, %Y"}
{$yesterday|date_format:$config.time}

   </pre>
<p>
    This above will output:
   </p>
<pre class="screen">

Jan 1, 2022
01/01/22
02:33 pm
Dec 31, 2021
Monday, December 1, 2021
14:33:00

   </pre>
</div>
</div>
<br class="example-break"><p>

  <span class="command"><strong>date_format</strong></span> conversion specifiers:
   </p>
<div class="itemizedlist"><ul class="itemizedlist" type="disc">
<li class="listitem"><p>
     %a - abbreviated weekday name according to the current locale
    </p></li>
<li class="listitem"><p>
     %A - full weekday name according to the current locale
    </p></li>
<li class="listitem"><p>
     %b - abbreviated month name according to the current locale
    </p></li>
<li class="listitem"><p>
     %B - full month name according to the current locale
    </p></li>
<li class="listitem"><p>
     %c - preferred date and time representation for the current locale
    </p></li>
<li class="listitem"><p>
     %C - century number (the year divided by 100 and truncated to an integer, range 00 to 99)
    </p></li>
<li class="listitem"><p>
     %d - day of the month as a decimal number (range 01 to 31)
    </p></li>
<li class="listitem"><p>
     %D - same as %m/%d/%y
    </p></li>
<li class="listitem"><p>
     %e - day of the month as a decimal number, a single digit is preceded by a space (range 1
     to 31)
    </p></li>
<li class="listitem"><p>
     %g - Week-based year within century [00,99]
    </p></li>
<li class="listitem"><p>
     %G - Week-based year, including the century [0000,9999]
    </p></li>
<li class="listitem"><p>
     %h - same as %b
    </p></li>
<li class="listitem"><p>
     %H - hour as a decimal number using a 24-hour clock (range 00 to 23)
    </p></li>
<li class="listitem"><p>
     %I - hour as a decimal number using a 12-hour clock (range 01 to 12)
    </p></li>
<li class="listitem"><p>
     %j - day of the year as a decimal number (range 001 to 366)
    </p></li>
<li class="listitem"><p>
     %k - Hour (24-hour clock) single digits are preceded by a blank. (range 0 to 23)
    </p></li>
<li class="listitem"><p>
     %l - hour as a decimal number using a 12-hour clock, single digits preceeded by a space
     (range 1 to 12)
    </p></li>
<li class="listitem"><p>
     %m - month as a decimal number (range 01 to 12)
    </p></li>
<li class="listitem"><p>
     %M - minute as a decimal number
    </p></li>
<li class="listitem"><p>
     %n - newline character
    </p></li>
<li class="listitem"><p>
     %p - either `am' or `pm' according to the given time value, or the corresponding strings
     for the
     current locale
    </p></li>
<li class="listitem"><p>
     %r - time in a.m. and p.m. notation
    </p></li>
<li class="listitem"><p>
     %R - time in 24 hour notation
    </p></li>
<li class="listitem"><p>
     %S - second as a decimal number
    </p></li>
<li class="listitem"><p>
     %t - tab character
    </p></li>
<li class="listitem"><p>
     %T - current time, equal to %H:%M:%S
    </p></li>
<li class="listitem"><p>
     %u - weekday as a decimal number [1,7], with 1 representing Monday
    </p></li>
<li class="listitem"><p>
     %U - week number of the current year as a decimal number, starting with the first Sunday
     as the first
     day of the first week
    </p></li>
<li class="listitem"><p>
     %V - The ISO 8601:1988 week number of the current year as a decimal number, range 01 to
     53, where week
     1 is the first week that has at least 4 days in the current
     year, and with Monday as the first day of the week.
    </p></li>
<li class="listitem"><p>
     %w - day of the week as a decimal, Sunday being 0
    </p></li>
<li class="listitem"><p>
     %W - week number of the current year as a decimal number, starting with the first Monday
     as the first
     day of the first week
    </p></li>
<li class="listitem"><p>
     %x - preferred date representation for the current locale without the time
    </p></li>
<li class="listitem"><p>
     %X - preferred time representation for the current locale without the date
    </p></li>
<li class="listitem"><p>
     %y - year as a decimal number without a century (range 00 to 99)
    </p></li>
<li class="listitem"><p>
     %Y - year as a decimal number including the century
    </p></li>
<li class="listitem"><p>
     %Z - time zone or name or abbreviation
    </p></li>
<li class="listitem"><p>
     %% - a literal `%' character
    </p></li>
</ul></div>
<p>

  </p>
<p>
   See also <a class="link" href="language.variables.smarty.html#language.variables.smarty.now" title="{$smarty.now}"><em class="parameter"><code>$smarty.now</code></em></a>,
   <a class="ulink" href="http://php.net/strftime" target="_top"><code class="varname">strftime()</code></a>,
   <a class="link" href="language.function.html.select.date.html" title="{html_select_date}"><code class="varname">{html_select_date}</code></a>
   and the <a class="link" href="tips.dates.html" title="Dates">date tips</a> page.
  </p>
</div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="language.modifier.count.words.html">Prev</a> </td>
<td width="20%" align="center"><a accesskey="u" href="language.modifiers.html">Up</a></td>
<td width="40%" align="right"> <a accesskey="n" href="language.modifier.default.html">Next</a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">count_words </td>
<td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td>
<td width="40%" align="right" valign="top"> default</td>
</tr>
</table>
</div>
</body>
</html>
